package db_main

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_main"
)

func GetIntimacyParam(param map[string]interface{}) (*models_main.Intimacy, error) {
	fName := "GetIntimacyParam"
	o := &models_main.Intimacy{}
	table := mysql.GetRTable(&models_main.Intimacy{})
	mysql.Condition(table, param, "", "", 0, 0)
	err := table.Find(o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func GetIntimacysParam(param map[string]interface{}, order string, page, size int) ([]*models_main.Intimacy, error) {
	fName := "GetIntimacysParam"
	o := make([]*models_main.Intimacy, 0)
	table := mysql.GetRTable(&models_main.Intimacy{})
	mysql.Condition(table, param, "", order, page, size)
	err := table.Find(&o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}
func InsertIntimacyByTransactions(session *gorm.DB, intimacy *models_main.Intimacy) error {
	fName := "InsertIntimacyByTransactions"
	err := session.Create(intimacy).Error
	if err != nil {
		session.Rollback()
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}

func UpdateIntimacyByTransactions(session *gorm.DB, columns []string, intimacy *models_main.Intimacy) error {
	fName := "UpdateIntimacyByTransactions"
	err := session.Where("id = ?", intimacy.Id).Select(columns).Updates(intimacy).Error
	if err != nil {
		session.Rollback()
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}
